package task3;

import java.sql.*;

public class userdao {
    //添加
    public static boolean insert(String username,String password){
        PreparedStatement ps = null;
        Connection conn=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            try {
                conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/userinfo","root","Aa1269630309");
                String sql = "insert into userinfo values(?,?)";
                 ps = conn.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);
                int line = ps.executeUpdate();
                if(line>0){
                    return true;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
                try {
                    if(ps==null) ps.close();
                    if(conn==null) conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        return false;
    }
    //查重
    public static boolean select1(String username){
        PreparedStatement ps = null;
        Connection conn=null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/userinfo","root","Aa1269630309");
            String sql = "select password from userinfo where username=?;";
                ps = conn.prepareStatement(sql);
                ps.setString(1,username);
                rs = ps.executeQuery();
                while(rs.next()){
                    return true;
                }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    //登录
    public static boolean select2(String username,String password){
        PreparedStatement ps = null;
        Connection conn=null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/userinfo","root","Aa1269630309");
            String sql = "select password from userinfo where username=?;";
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            while(rs.next()){
                if(password.equals(rs.getString("password"))) return true;
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public static boolean changePassword(String username,String newPassword){
        PreparedStatement ps = null;
        Connection conn=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/userinfo","root","Aa1269630309");
            String sql = "update userinfo set password = ? where username = ?;";
            ps = conn.prepareStatement(sql);
            ps.setString(2,username);
            ps.setString(1,newPassword);
            int line = ps.executeUpdate();
            if(line>0){
                return true;
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
}
